package com.huo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huo.constant.MessageConstant;
import com.huo.entity.PageResult;
import com.huo.entity.QueryPageBean;
import com.huo.entity.Result;
import com.huo.pojo.CheckItem;
import com.huo.service.CheckitemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:    检查项控制层
 * @Author:        wangqingsong
 * @CreateDate:     2020/12/17 15:17
 */
@RestController
@RequestMapping("/checkitem")
public class CheckitemController {

    @Reference
    private CheckitemService checkitemService;

    /**
     * @Description:    检查项新增
     * @Author:        wangqingsong
     * @param:           [checkItem]
     * @return:          com.huo.entity.Result
     * @CreateDate:     2020/12/17 15:16
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_ADD')")   //权限校验
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkitemService.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * @Description:    检查项分页
     * @Author:        wangqingsong
     * @param:           [queryPageBean]
     * @return:          com.huo.entity.PageResult
     * @CreateDate:     2020/12/19 13:43
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")   //权限校验
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkitemService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * @Description:    检查项删除
     * @Author:        wangqingsong
     * @param:           [id]
     * @return:          com.huo.entity.Result
     * @CreateDate:     2020/12/19 19:02
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")   //权限校验
    @RequestMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        try {
            checkitemService.deleteById(id);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * @Description:    根据id查询检查项
     * @Author:        wangqingsong
     * @param:           [id]
     * @return:          com.huo.entity.Result
     * @CreateDate:     2020/12/20 15:22
     */
    @RequestMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        try {
            CheckItem checkItem = checkitemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * @Description:    检查项编辑
     * @Author:        wangqingsong
     * @param:           [checkItem]
     * @return:          com.huo.entity.Result
     * @CreateDate:     2020/12/21 18:11
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_EDIT')")    //权限校验
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            checkitemService.edit(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }


    /**
     * @Description: 查询所有的检查项
     * @Author: 汪青松
     * @Param: []
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/15 17:18
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckItem> checkItemList = checkitemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
